package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSetingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSetingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSetingService.class)
@Transactional
public class OrderSetingServiceImpl implements OrderSetingService {
    @Autowired
    OrderSetingDao orderSetingDao;

    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if(orderSettingList != null && orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {
                long countByDate = orderSetingDao.findCountByDate(orderSetting.getOrderDate());
                if(countByDate>0){
                    //已经存在，修改
                    orderSetingDao.editOrderSetingByDate(orderSetting);
                }else{
                    //不存在，插入数据
                    orderSetingDao.insertOrderSeting(orderSetting);
                }
            }
        }
    }


    @Override
    public List<Map> findreservationsByDate(String date) {
        HashMap<String, String> stringHashMap = new HashMap<>();
        String begin = date+"-1";
        String end = date+"-31";
        stringHashMap.put("begin",begin);
        stringHashMap.put("end",end);
        List<OrderSetting> orderSettingList = orderSetingDao.findreservationsByDate(stringHashMap);
        List<Map> orderSettingMap = new ArrayList<>();
        for (OrderSetting orderSetting : orderSettingList) {
            HashMap<String, Object> stringObjectHashMap = new HashMap<>();
            //为符合前端需求，OrderSetting对象转Map,Map像是特殊版的OrderSetting对象，
            stringObjectHashMap.put("date",orderSetting.getOrderDate().getDate());
            stringObjectHashMap.put("number",orderSetting.getNumber());
            stringObjectHashMap.put("reservations",orderSetting.getReservations());
            orderSettingMap.add(stringObjectHashMap);
        }
        return orderSettingMap;
    }

    @Override
    public void editNumber(OrderSetting orderSetting) {
        long countByDate = orderSetingDao.findCountByDate(orderSetting.getOrderDate());
        if(countByDate>0){
            //已经有历史数据，只需更改
            orderSetingDao.editOrderSetingByDate(orderSetting);
        }else{
            //没有历史数据，属于新增
            orderSetingDao.insertOrderSeting(orderSetting);
        }
    }
}
